home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / FocusManager.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  137 lines

  1. /*
  2.  * @(#)FocusManager.java    1.5 98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. /**
  23.  * Swings Focus Manager
  24.  *
  25.  * @version 1.5 01/30/98
  26.  * @author Arnaud Weber
  27.  */
  28.  
  29. import java.util.Hashtable;
  30. import java.awt.event.KeyEvent;
  31. import java.awt.Component;
  32.  
  33. public abstract class FocusManager {
  34.  
  35.     /** This property name is used to get the FocusManager implementation
  36.      *  that should be used for a given UI
  37.      */
  38.     public static final String FOCUS_MANAGER_CLASS_PROPERTY = 
  39.         "FocusManagerClassName";
  40.  
  41.     private static final Object focusManagerKey = FocusManager.class;
  42.  
  43.     /** Return the FocusManager for the calling thread 
  44.      *  There is one FocusManager per thread group
  45.      */
  46.     public static FocusManager getCurrentManager() {
  47.         FocusManager result = 
  48.             (FocusManager)SwingUtilities.appContextGet(focusManagerKey);
  49.         if(result == null) {
  50.             String className = 
  51.                 UIManager.getString(FOCUS_MANAGER_CLASS_PROPERTY);
  52.             try {
  53.                 Class c = Class.forName(className);
  54.                 if(c != null) {
  55.                     result = (FocusManager) c.newInstance();
  56.                 }
  57.             } catch (ClassNotFoundException e) {
  58.                 System.out.println("Cannot find class " + className + " " + e);
  59.                 result = null;
  60.             } catch (InstantiationException e) {
  61.                 System.out.println("Cannot instantiate class " + className + " " + e);
  62.                 result = null;
  63.             } catch (IllegalAccessException e) {
  64.                 System.out.println("Cannot access class " + className + " " + e);
  65.                 result = null;
  66.             }
  67.             
  68.             if(result == null) {
  69.                 result = new DefaultFocusManager();
  70.             }
  71.             SwingUtilities.appContextPut(focusManagerKey, result);
  72.         }
  73.         return result;
  74.     }
  75.  
  76.     /** Set the FocusManager that should be used for the calling 
  77.      *  thread. <b>aFocusManager</b> will be the default focus
  78.      *  manager for the calling thread's thread group.
  79.      */
  80.     public static void setCurrentManager(FocusManager aFocusManager) {
  81.         if (aFocusManager != null) {
  82.             SwingUtilities.appContextPut(focusManagerKey, aFocusManager);
  83.         } else {
  84.             SwingUtilities.appContextRemove(focusManagerKey);
  85.         }
  86.     }
  87.  
  88.     /** Disable Swing's focus manager for the calling thread's thread group.
  89.      *  Call this method if your application mixes java.awt components and
  90.      *  swing's components. Your application will then use the awt focus 
  91.      *  manager.
  92.      */
  93.     public static void disableSwingFocusManager() {
  94.         setCurrentManager(new DisabledFocusManager());
  95.     }
  96.  
  97.     /** Return whether Swing's focus manager is enabled **/
  98.     public static boolean isFocusManagerEnabled() {
  99.         FocusManager fm = getCurrentManager();
  100.         return !(fm instanceof DisabledFocusManager);
  101.     }
  102.  
  103.     /** This method is called by JComponents when a key event occurs.
  104.      *  JComponent gives key events to the focus manager
  105.      *  first, then to key listeners, then to the keyboard UI dispatcher.
  106.      *  This method should look at the key event and change the focused
  107.      *  component if the key event matches the receiver's focus manager
  108.      *  hot keys. For example the default focus manager will change the
  109.      *  focus if the key event matches TAB or Shift + TAB.
  110.      *  The focus manager should call consume() on <b>anEvent</b> if 
  111.      *  <code>anEvent</code> has been processed. 
  112.      *  <code>focusedComponent</code> is the component that currently has
  113.      *  the focus.
  114.      *  Note: FocusManager will receive both KEY_PRESSED and KEY_RELEASED
  115.      *  key events. If one event is consumed, the other one should be consumed
  116.      *  too.
  117.      */
  118.     public abstract void processKeyEvent(Component focusedComponent,KeyEvent anEvent);
  119.  
  120.  
  121.     /** Cause the focus manager to set the focus on the next focusable component 
  122.      *  You can call this method to cause the focus manager to focus the next component
  123.      **/
  124.     public abstract void focusNextComponent(Component aComponent);
  125.  
  126.     /** Cause the focus manager to set the focus on the previous focusable component 
  127.      *  You can call this methid to cause the focus manager to focus the previous component
  128.      */
  129.     public abstract void focusPreviousComponent(Component aComponent);
  130.  
  131.     static class DisabledFocusManager extends FocusManager {
  132.         public void processKeyEvent(Component focusedComponent,KeyEvent anEvent) {}
  133.         public void focusNextComponent(Component c) {}
  134.         public void focusPreviousComponent(Component c) {}
  135.     }
  136. }
  137.